
package com.kugou.game.sdk.ui.dialog;

import com.kugou.game.sdk.utils.Rx;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 描述:进入游戏进度加载对话框
 * 
 * @author liux
 * @since 2014-8-17 下午01:09:41
 */
public class LoginDialog extends Dialog {

    private static LoginDialog loginingDialog = null;

    public LoginDialog(Context context) {
        super(context);
    }

    public LoginDialog(Context context, int theme) {
        super(context, theme);
    }

    public static LoginDialog initDialog(Context context) {
        loginingDialog = new LoginDialog(context, Rx.style.kg_CustomProgressDialog);
        loginingDialog.setContentView(Rx.layout.kg_login_progress_dialog);
        loginingDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        // 返回键是否退出
        loginingDialog.setCancelable(false);

        // 外面触摸是否退出
        loginingDialog.setCanceledOnTouchOutside(false);

        Window window = loginingDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        // 设置透明度
        lp.alpha = 0.6f;
        window.setAttributes(lp);
        // window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
        // WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        return loginingDialog;
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        if (loginingDialog == null) {
            return;
        }
        ImageView imageView = (ImageView) loginingDialog.findViewById(Rx.id.kg_iv_loading);
        Animation animation = AnimationUtils.loadAnimation(getContext(), Rx.anim.kg_login_loading);
        imageView.startAnimation(animation);
    }

    /**
     * 设置登录名
     */
    public LoginDialog setLoginUserName(String strMessage) {
        TextView tvMsg = (TextView) loginingDialog.findViewById(Rx.id.kg_tv_username);

        if (tvMsg != null) {
            tvMsg.setText(strMessage);
        }

        return loginingDialog;
    }

    /**
     * 点击对话框的切换账号按钮
     * 
     * @param listener
     * @return
     */
    public LoginDialog setOnClickAccountChangeListener(final OnClickAccountChangeListener listener) {
        TextView tvAccountChange = (TextView) loginingDialog
                .findViewById(Rx.id.kg_tv_change_account);
        tvAccountChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.clickChangeAccount();
                }
            }
        });
        return loginingDialog;
    }

    public interface OnClickAccountChangeListener {
        public void clickChangeAccount();
    }
}
